<template>
	<view class="page">

		<view class="page-top">
			<uni-segmented-control :current="current" :values="items" :style-type="styleType"
				:active-color="activeColor" @clickItem="onClickItem" />
		</view>
		<view class="content">
			<!-- <view v-if="current === 0"><text class="content-text">选项卡1的内容</text></view> -->
			<!-- <view v-if="current === 1"><text class="content-text">选项卡2的内容</text></view>
			<view v-if="current === 2"><text class="content-text">选项卡3的内容</text></view>
			<view v-if="current === 3"><text class="content-text">选项卡4的内容</text></view>
			<view v-if="current === 4"><text class="content-text">选项卡5的内容</text></view> -->
			<view class="order-list" v-if="list.length>0">
				<view class="order" v-for="item in list" :key="item.id">
					<view class="item-top">
						<view class="itemId">订单编号：{{item.orderId}}</view>
						<view class="itemStatus">{{orderStatus[item.status]}}</view>
					</view>
					<view class="item-con">
						<view class="item-info">
							<view class="item-img">
								<image :src="item.oimg" mode=""></image>
							</view>
							<view class="item-name">{{item.otitle}}</view>
						</view>
						<!-- <view class="item-price">￥{{item.payPrice}}</view> -->
					</view>
					<view class="item-money">实付款：<text class="item-number">￥{{item.payPrice}}</text></view>
					<view class="buttons" v-if="item.status===0">
						<view class="button cancel" @click="handleCancel(item.orderId)">申请取消</view>
						<!-- <view class="button pay" @click="toPay(item.orderId)">立即付款</view> -->
					</view>
				</view>
			</view>
			<view class="empty-box" v-else>
				暂无数据！
			</view>
		</view>


	</view>
</template>

<script>
	import {
		getOrderList,
		payOrder
	} from '@/api/order'
	import {
		cancelOrderHandle,
		payOrderHandle
	} from '@/libs/order'
	export default {
		data() {
			return {
				query: {
					limit: 10,
					page: 1,
					type: -1
				},
				orderStatus: {
					'-1': '已取消',
					'0': '待支付',
					'4': '已支付',
				},
				list: [],
				items: ['全部', '待付款', '已支付', '已取消'],
				status: 0,
				current: 0,
				activeColor: '#007aff',
				styleType: 'text',
			}
		},
		onLoad(options) {
			this.current = options.index * 1;
			this.query.type = options.status * 1;
			this.getList();
		},
		methods: {
			getList() {
				getOrderList(this.query).then((res) => {
					this.list = res.data;
				})
			},
			handleCancel(orderId) {
				cancelOrderHandle(orderId).then(() => {
					this.getList();
				}).catch(() => {
					// this.getList();
				});
			},
			toPay(orderId) {
				uni.showLoading({
					title: "支付中",
					mask: true
				});
				payOrder(orderId, 'weixin', 'wechat')
					.then(res => {
						try{
							let payData=res.data.result.jsConfig;
							//if(payUrl)window.location.href=payUrl;
							if(payData)this.onBridgeReady(payData);
						}catch(e){
							//TODO handle the exception
						}
						uni.hideLoading()
					})
					.catch(err => {
						uni.hideLoading()
						uni.showToast({
							title: err.msg || err.response.data.msg || err.response.data.message || '订单支付失败',
							icon: "none",
							duration: 2000,
						});
					});
			},
			onBridgeReady(payData){
				let _this=this;
			   WeixinJSBridge.invoke(
			      'getBrandWCPayRequest', payData,
			      function(res){
					  console.log("支付状态："+res.err_msg);
			      if(res.err_msg == "get_brand_wcpay_request:ok" ){
			      // 使用以上方式判断前端返回,微信团队郑重提示：
			            //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
						uni.showToast({
							title:'支付成功'
						})
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
			      } 
			   }); 
			},
			onClickItem(e) {

				if (e.currentIndex === 0) {
					this.query.type = -1;
				} else if (e.currentIndex === 1) {
					this.query.type = 0;
				} else if (e.currentIndex === 2) {
					this.query.type = 4;
				} else if (e.currentIndex === 3) {
					this.query.type = -2;
				}
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
					this.getList();
				}
			},
		}
	}
</script>

<style lang="scss">
	.page-top {
		background-color: #fff;
	}

	.empty-box {
		margin-top: 100rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		color: #808080;
		font-size: 28rpx;
	}

	.order {
		margin: 20rpx 30rpx;
		background-color: #fff;
		border-radius: 24rpx;
		padding: 0 30rpx 30rpx;
	}

	.item-top {
		padding-top: 42rpx;
		display: flex;
		justify-content: space-between;

		.itemId {
			font-size: 26rpx;
			font-weight: 500;
			color: #888888;
		}

		.itemStatus {
			font-size: 30rpx;
			font-weight: 500;
			color: #333333;
		}
	}

	.item-con {
		padding-top: 30rpx;

		.item-info {
			display: flex;
		}

		.item-img {
			margin-right: 22rpx;
			width: 157rpx;
			height: 157rpx;
			background: #D9D9D9;
			border-radius: 12rpx;
			flex-shrink: 0;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.item-name {
			flex: 1;
		}

		.item-name .item-price {
			font-size: 28rpx;
			font-weight: bold;
		}
	}

	.item-money {
		text-align: right;

		.item-number {
			color: #E94348;
		}
	}

	.buttons {
		display: flex;
		justify-content: flex-end;
	}

	.button {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 175rpx;
		height: 67rpx;
		border-radius: 50rpx;
		border: 1.2rpx solid #D3D3D3;
		text-align: center;
		font-size: 28rpx;
		font-weight: 500;
		margin: 30rpx 8rpx 0;
	}

	.pay {
		background-color: #0F7CFF;
		border: 1.2rpx solid #0F7CFF;
		color: #fff;
	}
</style>
